

#import <objc/runtime.h>

#import "UIView+MMLayout1.h"

#pragma mark - MMLayout
@interface MMLayout1 : NSObject
/*
 @LayoutView   传入进去  View
 */
- (instancetype)initWithLayoutView:(UIView *)LayoutView;
@property(assign, nonatomic) CGFloat top1;     ///<<  frame y
@property(assign, nonatomic) CGFloat bottom1;  ///<<  y = super.h - LayoutView.h - bottom
@property(assign, nonatomic) CGFloat left1;    ///<<  frame x
@property(assign, nonatomic) CGFloat right1;   ///<<  x = super.w - LayoutView.h - right
@property(assign, nonatomic) CGFloat width1;   ///<<  frame w
@property(assign, nonatomic) CGFloat height1;  ///<<  frame height
@property(assign, nonatomic) CGSize  size1;    ///<<  frame bounds size  width height
@property(assign, nonatomic) CGPoint point1;   ///<<  frame point
- (void)center1;                               ///<<调用此方法前必须先设置自己的宽高   (默认是居中父控件)
- (void)centerX1;                              ///<<调用此方法前必须先设置自己的宽   (默认是居中父控件)
- (void)centerY1;                              ///<<调用此方法前必须先设置自己的高   (默认是居中父控件)
@property(weak, nonatomic) UIView *layoutView1;
@end
@implementation MMLayout1
- (instancetype)initWithLayoutView:(UIView *)LayoutView {
    if (self = [super init]) {
        self.layoutView1 = LayoutView;
    }
    return self;
}
- (void)setLeft1:(CGFloat)left {
    CGRect frame          = self.layoutView1.frame;
    frame.origin.x        = left;
    self.layoutView1.frame = frame;
}
- (CGFloat)left1 {
    return self.layoutView1.frame.origin.x;
}
- (void)setTop1:(CGFloat)top1 {
    _top1                  = top1;
    CGRect frame          = self.layoutView1.frame;
    frame.origin.y        = top1;
    self.layoutView1.frame = frame;
}
- (void)setRight1:(CGFloat)right1 {
    UIView *superview    = self.layoutView1.superview;
    self.layoutView1.mm_x1 = superview.mm_w1 - self.layoutView1.mm_w1 - right1;
}
- (CGFloat)right1 {
    UIView *superview = self.layoutView1.superview;
    return superview.mm_w1 - self.layoutView1.mm_x1 - self.layoutView1.mm_w1;
}

- (void)setBottom1:(CGFloat)bottom1 {
    UIView *superview    = self.layoutView1.superview;
    self.layoutView1.mm_y1 = superview.mm_h1 - self.layoutView1.mm_h1 - bottom1;
}

- (CGFloat)bottom1 {
    UIView *superview = self.layoutView1.superview;
    return superview.mm_h1 - self.layoutView1.mm_maxY1;
}

- (void)setHeight1:(CGFloat)height1 {
    _height1               = height1;
    CGRect frame          = self.layoutView1.frame;
    frame.size.height     = height1;
    self.layoutView1.frame = frame;
}
- (void)setWidth1:(CGFloat)width1 {
    _width1                = width1;
    CGRect frame          = self.layoutView1.frame;
    frame.size.width      = width1;
    self.layoutView1.frame = frame;
}
- (void)setPoint1:(CGPoint)point1 {
    CGRect frame          = self.layoutView1.frame;
    frame.origin          = point1;
    self.layoutView1.frame = frame;
}
- (CGPoint)point1 {
    return CGPointMake(self.layoutView1.mm_x1, self.layoutView1.mm_w1);
}
- (void)setSize1:(CGSize)size1 {
    CGRect frame          = self.layoutView1.frame;
    frame.size            = size1;
    self.layoutView1.frame = frame;
}
- (CGSize)size1 {
    return self.layoutView1.mm_size1;
}

- (void)setCenter1:(CGPoint)center1 {
    CGRect frame          = self.layoutView1.frame;
    frame.origin          = CGPointMake(center1.x - frame.size.width / 2, center1.y - frame.size.height / 2);
    self.layoutView1.frame = frame;
}
- (void)center1 {
    UIView *superview    = self.layoutView1.superview;
    self.layoutView1.mm_x1 = superview.mm_halfW1 - self.layoutView1.mm_halfW1;
    self.layoutView1.mm_y1 = superview.mm_halfH1 - self.layoutView1.mm_halfH1;
}
- (void)centerY1 {
    UIView *superview    = self.layoutView1.superview;
    self.layoutView1.mm_y1 = superview.mm_halfH1 - self.layoutView1.mm_halfH1;
}
- (void)centerX1 {
    UIView *superview    = self.layoutView1.superview;
    self.layoutView1.mm_x1 = superview.mm_halfW1 - self.layoutView1.mm_halfW1;
}
@end
const void *    _layoutKey;
@implementation UIView (Layout)
#pragma mark - frame
- (void)setMm_x1:(CGFloat)mm_x1 {
    [self mm_selfLayout1].left1 = mm_x1;
}
- (CGFloat)mm_x1 {
    return self.frame.origin.x;
}
- (void)setMm_y1:(CGFloat)mm_y1 {
    [self mm_selfLayout1].top1 = mm_y1;
}
- (CGFloat)mm_y1 {
    return self.frame.origin.y;
}
- (void)setMm_w1:(CGFloat)mm_w1 {
    [self mm_selfLayout1].width1 = mm_w1;
}
- (CGFloat)mm_w1 {
    return self.frame.size.width;
}
- (void)setMm_h1:(CGFloat)mm_h1 {
    [self mm_selfLayout1].height1 = mm_h1;
}
- (CGFloat)mm_h1 {
    return self.frame.size.height;
}
- (void)setMm_center1:(CGPoint)mm_center1 {
    [self mm_selfLayout1].point1 = mm_center1;
}
- (CGPoint)mm_center1 {
    return self.frame.origin;
}
- (CGFloat)mm_centerX1 {
    return self.center.x;
}
- (CGFloat)mm_centerY1 {
    return self.center.y;
}
- (CGFloat)mm_maxY1 {
    return CGRectGetMaxY(self.frame);
}
- (CGFloat)mm_minY1 {
    return CGRectGetMinY(self.frame);
}
- (CGFloat)mm_maxX1 {
    return CGRectGetMaxX(self.frame);
}
- (CGFloat)mm_minX1 {
    return CGRectGetMinX(self.frame);
}
- (CGFloat)mm_halfW1 {
    return self.mm_w1 / 2;
}
- (CGFloat)mm_halfH1 {
    return self.mm_h1 / 2;
}
- (CGFloat)mm_halfX1 {
    return self.mm_x1 / 2;
}
- (CGFloat)mm_halfY1 {
    return self.mm_y1 / 2;
}
- (CGFloat)mm_halfCenterX1 {
    return self.mm_centerX1 / 2;
}
- (CGFloat)mm_halfCenterY1 {
    return self.mm_centerY1 / 2;
}
- (void)setMm_size1:(CGSize)mm_size1 {
    [self mm_selfLayout1].size1 = mm_size1;
}
- (CGSize)mm_size {
    return self.bounds.size;
}
#pragma mark - set_ frame
- (MMLayout1 *)mm_selfLayout1 {
    MMLayout1 *layout = objc_getAssociatedObject(self, &_layoutKey);
    if (layout == nil) {
        layout = [[MMLayout1 alloc] initWithLayoutView:self];
        objc_setAssociatedObject(self, &_layoutKey, layout, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return layout;
}
- (UIView * (^)(CGFloat))m_top1 {
    @m_weakify(self);
    return ^(CGFloat m_top1) {
        @m_strongify(self);
        [self mm_selfLayout1].top1 = m_top1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_flexToTop1 {
    @m_weakify(self);
    return ^(CGFloat m_flexToTop1) {
        @m_strongify(self);
        CGFloat top              = [self mm_selfLayout1].top1;
        [self mm_selfLayout1].top1 = m_flexToTop1;
        [self mm_selfLayout1].height1 += top - m_flexToTop1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_bottom1 {
    @m_weakify(self);
    return ^(CGFloat m_bottom1) {
        @m_strongify(self);
        [self mm_selfLayout1].bottom1 = m_bottom1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_flexToBottom1 {
    @m_weakify(self);
    return ^(CGFloat m_flexToBottom1) {
        @m_strongify(self);
        CGFloat bottom = [self mm_selfLayout1].bottom1;
        [self mm_selfLayout1].height1 += bottom - m_flexToBottom1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_left1 {
    @m_weakify(self);
    return ^(CGFloat m_left1) {
        @m_strongify(self);
        [self mm_selfLayout1].left1 = m_left1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_flexToLeft1 {
    @m_weakify(self);
    return ^(CGFloat m_flexToLeft1) {
        @m_strongify(self);
        CGFloat left              = [self mm_selfLayout1].left1;
        [self mm_selfLayout1].left1 = m_flexToLeft1;
        [self mm_selfLayout1].width1 += left - m_flexToLeft1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_right1 {
    @m_weakify(self);
    return ^(CGFloat m_right1) {
        @m_strongify(self);
        [self mm_selfLayout1].right1 = m_right1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_flexToRight1 {
    @m_weakify(self);
    return ^(CGFloat m_flexToRight1) {
        @m_strongify(self);
        CGFloat right = [self mm_selfLayout1].right1;
        [self mm_selfLayout1].width1 += right - m_flexToRight1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_width1 {
    @m_weakify(self);
    return ^(CGFloat m_width1) {
        @m_strongify(self);
        [self mm_selfLayout1].width1 = m_width1;
        return self;
    };
}
- (UIView * (^)(CGFloat))m_height1 {
    @m_weakify(self);
    return ^(CGFloat m_height1) {
        @m_strongify(self);
        [self mm_selfLayout1].height1 = m_height1;
        return self;
    };
}
- (UIView * (^)(CGSize))m_size1 {
    @m_weakify(self);
    return ^(CGSize m_size1) {
        @m_strongify(self);
        [self mm_selfLayout1].size1 = m_size1;
        return self;
    };
}
- (UIView * (^)(CGPoint))m__center1 {
    @m_weakify(self);
    return ^(CGPoint m__center1) {
        @m_strongify(self);
        [self mm_selfLayout1].center1 = m__center1;
        return self;
    };
}
- (UIView * (^)(void))m_center1 {
    @m_weakify(self);
    return ^{
        @m_strongify(self);
        [[self mm_selfLayout1] center1];
        return self;
    };
}

- (UIView * (^)(void))m_centerY1 {
    @m_weakify(self);
    return ^{
        @m_strongify(self);
        [[self mm_selfLayout1] centerY1];
        return self;
    };
}

- (UIView * (^)(void))m_centerX1 {
    @m_weakify(self);
    return ^{
        @m_strongify(self);
        [[self mm_selfLayout1] centerX1];
        return self;
    };
}

- (UIView * (^)(void))m_fill1 {
    @m_weakify(self);
    return ^{
        @m_strongify(self);
        if (self.superview) {
            self.mm_x1 = self.mm_y1 = 0;
            self.mm_w1             = self.superview.mm_w1;
            self.mm_h1             = self.superview.mm_h1;
        }
        return self;
    };
}

#pragma mark -  m_equalTo
- (UIView * (^)(UIView *))m_equalToFrame1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.frame = obj.frame;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToTop1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.top1 = obj.mm_selfLayout1.top1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToBottom1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.bottom1 = obj.mm_selfLayout1.bottom1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToLeft1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.left1 = obj.mm_selfLayout1.left1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToRight1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.right1 = obj.mm_selfLayout1.right1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToWidth1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.width1 = obj.mm_selfLayout1.width1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToHeight1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        self.mm_selfLayout1.height1 = obj.mm_selfLayout1.height1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToSize1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        [self mm_selfLayout1].size1 = obj.mm_selfLayout1.size1;
        return self;
    };
}
- (UIView * (^)(UIView *))m_equalToCenter1 {
    @m_weakify(self);
    return ^(UIView *obj) {
        @m_strongify(self);
        [self mm_selfLayout1].point1 = obj.mm_selfLayout1.point1;
        return self;
    };
}

- (UIView * (^)(void))m_sizeToFit1 {
    @m_weakify(self);
    return ^(void) {
        @m_strongify(self);
        [self sizeToFit];
        return self;
    };
}

- (NSData *)mm_createPDF1 {
    CGRect            bounds   = self.bounds;
    NSMutableData *   data     = [NSMutableData data];
    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
    CGContextRef      context  = CGPDFContextCreate(consumer, &bounds, NULL);
    CGDataConsumerRelease(consumer);
    if (!context) return nil;
    CGPDFContextBeginPage(context, NULL);
    CGContextTranslateCTM(context, 0, bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    [self.layer renderInContext:context];
    CGPDFContextEndPage(context);
    CGPDFContextClose(context);
    CGContextRelease(context);
    return data;
}

- (UIViewController *)viewController1 {
    UIView *view = self;
    while (view) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
        view = view.superview;
    }
    return nil;
}

static void *kUIViewLayoutMethodPropertyBottomGap = &kUIViewLayoutMethodPropertyBottomGap;
static void *kUIViewLayoutMethodPropertyTopGap    = &kUIViewLayoutMethodPropertyTopGap;
static void *kUIViewLayoutMethodPropertyLeftGap   = &kUIViewLayoutMethodPropertyLeftGap;
static void *kUIViewLayoutMethodPropertyRightGap  = &kUIViewLayoutMethodPropertyRightGap;

- (CGFloat)mm_safeAreaBottomGap1 {
    NSNumber *gap = objc_getAssociatedObject(self, kUIViewLayoutMethodPropertyBottomGap);
    if (gap == nil) {
        if (@available(iOS 11, *)) {
            if (self.superview.safeAreaLayoutGuide.layoutFrame.size.height > 0) {
                gap = @((self.superview.mm_h1 - self.superview.safeAreaLayoutGuide.layoutFrame.origin.y - self.superview.safeAreaLayoutGuide.layoutFrame.size.height));
            } else {
                gap = nil;
            }
        } else {
            gap = @(0);
        }
        objc_setAssociatedObject(self, kUIViewLayoutMethodPropertyBottomGap, gap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return gap.floatValue;
}

- (CGFloat)mm_safeAreaTopGap1 {
    NSNumber *gap = objc_getAssociatedObject(self, kUIViewLayoutMethodPropertyTopGap);
    if (gap == nil) {
        if (@available(iOS 11, *)) {
            gap = @(self.superview.safeAreaLayoutGuide.layoutFrame.origin.y);
        } else {
            gap = @(0);
        }
        objc_setAssociatedObject(self, kUIViewLayoutMethodPropertyTopGap, gap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return gap.floatValue;
}

- (CGFloat)mm_safeAreaLeftGap1 {
    NSNumber *gap = objc_getAssociatedObject(self, kUIViewLayoutMethodPropertyLeftGap);
    if (gap == nil) {
        if (@available(iOS 11, *)) {
            gap = @(self.superview.safeAreaLayoutGuide.layoutFrame.origin.x);
        } else {
            gap = @(0);
        }
        objc_setAssociatedObject(self, kUIViewLayoutMethodPropertyLeftGap, gap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return gap.floatValue;
}

- (CGFloat)mm_safeAreaRightGap1 {
    NSNumber *gap = objc_getAssociatedObject(self, kUIViewLayoutMethodPropertyRightGap);
    if (gap == nil) {
        if (@available(iOS 11, *)) {
            gap = @((self.superview.mm_w1 - self.superview.safeAreaLayoutGuide.layoutFrame.origin.x - self.superview.safeAreaLayoutGuide.layoutFrame.size.width));
        } else {
            gap = @(0);
        }
        objc_setAssociatedObject(self, kUIViewLayoutMethodPropertyRightGap, gap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return gap.floatValue;
}

@end
